function graphsVec=plotIRF(irfMat,inStru,nameStru)
% =========================================================================
% plotIRF(irfMAT,nameStru);
%
% 1) *irfMat*    [ns nx nperiods] matrix of *impulse responses* data.
%                Optional argument, if not passed then the observable will be
%                taken to be the sum of the cmat matrix
%
% 2) *inStru*      Structure with inputs for graphing as well as 
%                  additional inputs 
%
%    *.colExtract* Names of these columns to be plotted only and extracted
%                  from the toal columns
%    *.addToTitle* String to add to the title 
% 
%    *.NRow*       Number of rows per graph 
%    *.NCols*      Number of columns per graph 
%    Defaults will be based on the number of series 
%
% 3)  *nameStru* Structure 
% 
%    nameStru.colNames   Names of columns (shocks)
%    nameStru.rowNames   Names of rows    (series) 
%   
% Alejandro Justiniano January 21st 2010
% =========================================================================
dim=size(irfMat); 
%% 3. Defaults%
inStru=plotAssignDefaults(inStru);
%% 2. Check sizes
if length(nameStru.rowNames)~=dim(1); 
    error('rowNames does not match row dimension'); 
end 
if length(nameStru.colNames)~=dim(2); 
    error('colNames does not match column dimension'); 
end 
%% 3. Extract Columns 
if isfield(inStru,'colExtract'); 
    colExtract=cellposition(inStru.colExtract,nameStru.colNames); 
    inStru.colNames=colExtract; 
    irfMat=irfMat(:,colExtract,:); 
    dim=size(irfMat); 
end 
%% 4. Assign Fields to inStru 
if ~isempty(inStru.axesFontSize); 
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end 
inStru.flagNoLegend=1; 
inStru.legendFontSize=12; 
inStru.xOffset=0.05; 
inStru.yOffset=0.05; 
inStru.flagBoxOff=1; 
inStru.lineWidths=2;
inStru.colors=[0 0 0]; 

%% Graph Settings 
if isfield(inStru,'NRows')==false || isfield(inStru,'NCols')==false
    if dim(1) <= 12
        inStru.NCols=2;
        inStru.flagLanscape=0;
    elseif dim(1) > 12 && dim(1) <= 16;
        inStru.NCols=3;
        inStru.flagLanscape=0;
    elseif dim(1) > 16 && dim(1) <= 24;
        inStru.NCols=4;
        inStru.flagLanscape=0;
    elseif dim(1) > 24 && dim(1) <= 30
        inStru.NCols=6;
        inStru.flagLanscape=0;
    else
        error('Cannot plot more than 30 rows in a single graph')
    end
    inStru.NRows=ceil(dim(1)/inStru.NCols);
end
%% Settings for all plots
NGraphs=ceil(dim(1)/(inStru.NRows*inStru.NCols)); 
graphsVec=zeros(NGraphs,dim(2)); 
xVec=(0:dim(3)-1); 
NSubplots=dim(1);
%%Outer Loop
for jj=1:dim(2)
    ISubplots=0;
    IGraphs  =0;
    ICounter =0;
    %% Inner Loop
    for ii=1:NSubplots
        if ISubplots==0
            IGraphs=IGraphs+1;
            graphsVec(IGraphs,jj)=figure;
        end
        ISubplots=ISubplots+1;
        ICounter =ICounter+1;
        subplot(inStru.NRows,inStru.NCols,ISubplots);
        [plotHandle,legendHandle,figHandle]=plot3sampSub(xVec,squeeze(irfMat(ii,jj,:)),...
            nameStru.rowNames{ii},1,inStru);
        %% If I
        if ISubplots==inStru.NRows*inStru.NCols || ii==NSubplots
            ISubplots=0;
            if isfield(inStru,'addToTitle')==true
                titleStr=[nameStru.colNames{jj},' ',inStru.addToTitle,' IRF'];
            else
                titleStr=[nameStru.colNames{jj},' IRF'];
            end
            if NGraphs==1
                supHand=suptitle(titleStr);
            else
                supHand=suptitle([titleStr,'   (',num2str(IGraphs),' of ',...
                    num2str(NGraphs),')']);
            end
            set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
                'FontWeight',inStru.suptitleFontWeight);
            set(graphsVec(IGraphs,jj),'PaperPosition',inStru.paperPosition)
        end
    end
end
if ~isempty(inStru.axesFontSize);
    set(0,'DefaultAxesFontSize','remove');
end
